package com.gx.smart.construction.ui.area.activity;

import android.graphics.drawable.Drawable;
import android.preference.PreferenceDataStore;
import android.util.Log;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.datastore.core.DataStore;
import androidx.datastore.preferences.core.MutablePreferences;
import androidx.datastore.preferences.core.Preferences;
import androidx.datastore.preferences.core.PreferencesKeys;
import androidx.datastore.preferences.rxjava3.RxPreferenceDataStoreBuilder;
import androidx.datastore.rxjava3.RxDataStore;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DividerItemDecoration;

import com.blankj.utilcode.util.SPUtils;
import com.gx.smart.base.BaseAdapter;
import com.gx.smart.base.BaseMVVMRefreshActivity;
import com.gx.smart.common.util.AppConfig;
import com.gx.smart.construction.R;
import com.gx.smart.construction.databinding.ActivityAreaBinding;
import com.gx.smart.construction.ui.area.adapter.AreaAdapter;
import com.gx.smart.construction.ui.area.viewmodel.AreaViewModel;
import com.gx.smart.construction.ui.common.util.DataStoreHelper;
import com.gx.wisestone.constructionappgrpclib.grpc.systenant.ConstructionAppSysTenantDto;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.List;

import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.functions.Consumer;

public class AreaActivity extends BaseMVVMRefreshActivity<ActivityAreaBinding, AreaViewModel>
        implements BaseAdapter.OnItemClickListener<ConstructionAppSysTenantDto> {

    private AreaAdapter adapter;

    @Override
    protected Class<AreaViewModel> onBindViewModel() {
        return AreaViewModel.class;
    }

    @Override
    protected ActivityAreaBinding onBindViewBinding(@NonNull LayoutInflater inflater) {
        return ActivityAreaBinding.inflate(inflater);
    }


    @Override
    protected void initView() {
        initTitle(binding.title);
        adapter = new AreaAdapter(this);
        adapter.setItemClickListener(this);
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        Drawable drawable = getResources().getDrawable(R.drawable.shape_list_item_divider_line);
        divider.setDrawable(drawable);
        binding.recyclerView.addItemDecoration(divider);
        initView(binding.recyclerView, adapter, binding.refreshLayout, binding.noViewData.getRoot());
    }

    @Override
    protected void initData() {
        binding.refreshLayout.autoRefresh();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        super.onRefresh(refreshlayout);
        viewModel.SysTenantList(currentPage);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        super.onLoadMore(refreshLayout);
        viewModel.SysTenantList(currentPage);
    }

    @Override
    protected void initObserver() {
        viewModel.data.observe(this, new Observer<List<ConstructionAppSysTenantDto>>() {
            @Override
            public void onChanged(List<ConstructionAppSysTenantDto> list) {
                handleResult(list);
            }
        });
    }

    @Override
    public void onItemClick(ConstructionAppSysTenantDto item, int position) {
        //sys_tenant_no
        SPUtils.getInstance().put(AppConfig.SYS_TENANT_NO, item.getSysTenantNo());
//        SPUtils.getInstance().put(AppConfig.AREA_NAME, item.getName());
        DataStoreHelper dataStoreHelper =  DataStoreHelper.getInstance();
        dataStoreHelper.writeValue(dataStoreHelper.AREA_NAME_KEY, item.getName());
        finish();
    }


}